from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
import time
class JdSpider:
    def __init__(self):
        self.url='http://www.jd.com/'
        self.options=webdriver.EdgeOptions()
        self.options.add_argument('--headless')
        browser=Service(r'D:\EdgeDriver\msedgedriver.exe')
        self.browser=webdriver.Edge(service=browser,options=self.options)
        self.i=0
    def get_html(self):
        self.browser.get(self.url)
        self.browser.find_element(by=By.XPATH,value='//*[@id="key"]').send_keys('python书籍')
        self.browser.find_element(by=By.XPATH,value='//*[@class="form"]/button').click()
    def get_data(self):
        self.browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        time.sleep(2)
        litags=self.browser.find_elements(by=By.XPATH,value='//*[@id="J_goodList"]/ul/li')
        for litag in litags:
            item={}
            item['name']=litag.find_element(by=By.XPATH,value='.//div[@class="p-name"]/a/em').text.strip()
            item['price']=litag.find_element(by=By.XPATH,value='.//div[@class="p-price"]').text.strip()
            item['count']=litag.find_element(by=By.XPATH,value='.//div[@class="p-commit"]/strong').text.strip()
            item['shop']=litag.find_element(by=By.XPATH,value='.//div[@class="p-shopnum"]').text.strip()
            print(item)
            self.i+=1
    def run(self):
        self.get_data()
        if self.browser.page_source.find('pn-next disabled') == -1:
            self.browser.find_element(by=By.CLASS_NAME,value='pn-next').click()
            time.sleep(1)
    def fetch(self,n=0):
        self.get_html()
        if n > 0:
            for i in range(n):
                self.run()
        else:
            while True:
                self.run()
                if self.browser.page_source.find('pn-next disabled') != -1:
                    break
        print('Goods Number: ', self.i)
if __name__ == '__main__':
    spider=JdSpider()
    spider.fetch(3)
